<?php
class SiteController extends Zend_Controller_Action{
	protected $_menuModel ;
	protected $_bannerModel;
	protected $_manufactureModel;
	protected $_pcategoryModel;
	protected $_productModel;
	protected $_postModel;
	protected $_supporterModel;
	protected $_filterModel;
	protected $_filterValueModel;
	protected $_categoryModel;
      protected $_tagModel;
	public function init(){
		
		//khởi tạo model
		$this->_menuModel = new Model_Menu();
		$this->_bannerModel = new Model_Banner();
		$this->_manufactureModel = new Model_Manufacturer();
		$this->_pcategoryModel = new Model_PCategory();
		$this->_supporterModel = new Model_Supporter();
		$this->_productModel = new Model_Product();
		$this->_postModel = new Model_Post();
		$this->_filterModel = new Model_Filter();
		$this->_filterValueModel = new Model_FilterValue();
		$this->_categoryModel = new Model_Category();
            $this->_tagModel = new Model_Tag();
            $menu = $this->_menuModel->getAllMenus();
			
		$this->view->menu = $menu;
            $banners = $this->_bannerModel->getAllBanners();
		//	$cache->save($banners, 'banners');
		//}
		$this->view->banners = $banners;
		//lấy mans
		//riêng trường hợp này không hiểu sao sử dụng cache có vấn đề làm sau
		//lấy dữ liệu về menu từ db
			$mans = $this->_manufactureModel->getAllManufacturers();
			//$manCategories = $mans[0]->findManyToManyRowset("Model_PCategory", "Model_ManCat");
			//$cache->save($mans, 'mans');
		//}
		
		$this->view->mans = $mans;
		//lấy category
		//$pCategories = $cache->load('pcategories');
		//if($pCategories === false ) {
		 	//lấy dữ liệu về menu từ db
			$pCategories = $this->_pcategoryModel->getAllCategories();
			//$cache->save($pCategories, 'pcategories');
		//}
		$this->view->pCategories = $pCategories;
		
		//lấy tài khoản hộ trợ trực tuyến
		//$supporters = $cache->load('supporters');
		//if($supporters === false ) {
		 	//lấy dữ liệu về menu từ db
			$supporters = $this->_supporterModel->getAllSupporters();
		//	$cache->save($supporters, 'supporters');
		//}
		$this->view->supporters = $supporters;
		
			// lấy tin footer
		//	try{
		//$info_cp = $cache->load('info_cp');
		//if($info_cp === false){
			//lấy data từ db
			
			$info_cps = $this->_postModel->getPostByCategory_ID(4);	
		//	$cache->save($info_cps,'info_cps');
		//}
		$this->view->info_cps = $info_cps;
		
		//$help_purchase = $cache->load('help_purchase');
		//if($help_purchase === false){
			
			$help_purchases = $this->_postModel->getPostByCategory_ID(5);
		//	$cache->save($help_purchases,'help_purchases');	
		//}
		$this->view->help_purchases = $help_purchases;
		
		//$policies = $cache->load('policies');
		//if($policies === false){
			
			$policies = $this->_postModel->getPostByCategory_ID(6);
		//	$cache->save($policies,'policies');
		//}
		$this->view->policies = $policies;
			//}catch (Exception $ex){
			//	echo $ex->getMessage();
		//}
		$this->view->tags = $this->_tagModel->getAllTags();
            		//lấy dữ liệu giỏ hàng
		$session = new Zend_Session_Namespace();
		$cart = $session->cart;
		
		if(isset($cart) && count($cart) > 0 ){
			$totalCartPrice = 0;
			$totalCartProduct = 0;
			foreach($cart as $product_id => $product){
				
				$totalCartPrice += $product["price"] * $product["quantity"];
				
				$totalCartProduct+= $product["quantity"];
			}
			$this->view->totalCartProduct = $totalCartProduct;
			$this->view->totalCartPrice = $totalCartPrice;
		}else{
			$this->view->totalCartProduct = 0;
			$this->view->totalCartPrice = 0;
		}
		$mapPage = $this->_postModel->getPostByID(1);
		$this->view->mapPage = $mapPage;
	}
}

